iT邦幫忙

2026 iThome 鐵人賽

DAY 14
0
AI Engineering

Harness Engineering × Pi Agent 實戰:打造可觀測、可評估的 AI Coding Agent系列 第 14 篇

Day14:System Prompt 真的是成本大戶嗎?拆解 Pi 的動態組裝與帳單

  • 分享至 

  • xImage
  •  

把最上面那一層打開

前面拆了 AGENTS.md(Day8)、Skills(Day10)、工具(Day12)。這些東西最後都會匯集到同一個地方:system prompt。今天把它整份打開來看。

Day6 提過一件事:session 記錄裡看不到 system prompt。它在迴圈開始前就被組好,每一輪都跟著送出去,但不會被存進 .jsonl。所以要看它長什麼樣子,得直接叫 Pi 自己把它組出來。

這就是全文

以下是預設狀態(開 read/bash/edit/write 四個工具、沒有 AGENTS.md、沒有 skills)下,Pi 送出去的完整 system prompt。我只把路徑裡的家目錄換成 <家目錄>,其餘一字未改:

You are an expert coding assistant operating inside pi, a coding agent harness. You help users by
reading files, executing commands, editing code, and writing new files.

Available tools:
- read: Read file contents
- bash: Execute bash commands (ls, grep, find, etc.)
- edit: Make precise file edits with exact text replacement, including multiple disjoint edits in one call
- write: Create or overwrite files

In addition to the tools above, you may have access to other custom tools depending on the project.

Guidelines:
- Use bash for file operations like ls, rg, find
- Use read to examine files instead of cat or sed.
- You can inspect PI_* environment variables for current model and session details.
- Use edit for precise changes (edits[].oldText must match exactly)
- Use write only for new files or complete rewrites.
- Be concise in your responses
- Show file paths clearly when working with files

Pi documentation (read only when the user asks about pi itself, its SDK, extensions, themes, skills, or TUI):
- Main documentation: <家目錄>\...\pi-coding-agent\README.md
- Additional docs: <家目錄>\...\pi-coding-agent\docs
- Examples: <家目錄>\...\pi-coding-agent\examples (extensions, custom tools, SDK)
- When reading pi docs or examples, resolve docs/... under Additional docs and examples/... under Examples,
  not the current working directory
- When asked about: extensions (docs/extensions.md, examples/extensions/), themes (docs/themes.md),
  skills (docs/skills.md), prompt templates (docs/prompt-templates.md), TUI components (docs/tui.md),
  keybindings (docs/keybindings.md), SDK integrations (docs/sdk.md), custom providers (docs/custom-provider.md),
  adding models (docs/models.md), pi packages (docs/packages.md), environment variables (docs/environment-variables.md)
- When working on pi topics, read the docs and examples, and follow .md cross-references before implementing
- Always read pi .md files completely and follow links to related docs (e.g., tui.md for TUI API details)
Current working directory: D:/work/taskapp

28 行、2,198 個字元,換算大約五百多個 token。一個能自己讀檔、跑指令、改程式碼的 coding agent,指令部分就這麼多。

五個區塊,各有各的用途

區塊 內容 誰決定
開場一句 你是誰、在什麼東西裡面跑 寫死
Available tools 每個工具一行的 snippet 當下開了哪些工具
Guidelines 怎麼用這些工具的規則 工具自己帶的 + 動態加的
Pi documentation Pi 自己的文件在哪 安裝路徑
Current working directory 工作目錄 執行時決定

有 AGENTS.md 的話,<project_context> 會接在 Guidelines 之後;有 skills 的話,<available_skills> 再接在後面;最後才是工作目錄那一行。

最值得學的一個設計:prompt 會跟著能力變

看 Guidelines 的第一行:

- Use bash for file operations like ls, rg, find

這行不是寫死的。原始碼裡它只有在「有 bash,但沒開 grep/find/ls」的時候才會加進去:

if ((hasBash || hasPowerShell) && !hasGrep && !hasFind && !hasLs) {
    addGuideline("Use bash for file operations like ls, rg, find");
}

同樣的道理,工具清單只列出「呼叫端有提供 snippet」的工具;skills 區塊只有在 read 工具開著時才會出現(Day10 提過)。

這就是 harness engineering 的一個具體樣貌:prompt 不是一份文件,是一個函式。輸入是「這次開了哪些能力」,輸出是「該告訴模型什麼」。能力不存在就不要提,提了只會讓模型嘗試不存在的東西;能力存在但不明顯,就補一行提示。

Day13 的實驗之所以不只是「多兩個工具」這麼單純,原因也在這裡——開啟 grep/find/ls 會同時移除那行「用 bash 做檔案操作」的提示。變的不只是工具清單,還有指令本身。

它在成本裡佔多少?

system prompt 每一輪都會重送,聽起來很貴。但實際量一下就會發現它不是大頭。

用第一輪校準的資料來看(gpt-5.6-luna,30 次執行):

  • 第一次模型呼叫的輸入是 1,120~1,171 tokens(system prompt + 工具的 JSON schema + 使用者的任務描述,五個任務略有差異)。
  • 加上專案的 AGENTS.md 之後變成 1,474~1,525 tokens。兩者相減,那份 1,145 字元的 AGENTS.md 值 354 個 token——五個任務量出來都是 354,一個不差。
  • 但整批執行下來,總輸入是 537,224 tokens。

換句話說:system prompt 只在第一輪佔比高,之後迅速被對話歷史稀釋。 真正把成本推上去的是「每一輪把目前為止讀過的檔案、跑過的指令結果整包重送」。

把 30 次執行的成本拆開來更清楚:

項目 tokens 成本 佔比
輸入(未命中快取) 537,224 $0.107 63%
輸出(含 reasoning) 36,154 $0.043 26%
輸入(命中快取) 953,344 $0.019 11%

有兩件事值得記住:

  1. 六成以上的錢花在輸入。 不是模型想太多,也不是寫太多程式碼,是「重送 context」。
  2. 命中快取的量比沒命中的還多(95 萬 vs 54 萬 tokens),但只佔成本的一成。 這個模型的快取價格是原價的十分之一。prompt cache 不是小優化,它是這類長迴圈任務能不能負擔得起的關鍵。

對 harness 設計的啟示

如果你想壓低一個 coding agent 的成本,從這份資料看,優先順序應該是:

  1. 讓快取命中率高:system prompt 和前面的對話盡量穩定不變,才可能命中。反過來說,在 prompt 中段插入會變動的內容(時間戳、隨機排序的檔案清單)會讓後面全部失效。
  2. 控制進入 context 的東西:read 的截斷、bash 輸出的上限(Day12)都是在做這件事。
  3. 最後才是縮短 system prompt。它只有 2,198 個字元,砍一半也只省下兩百多個 token,而且是唯一可能被快取的部分。

這也是為什麼 Day15 那個「把 system prompt 加料」的實驗,我事前的預期是成本影響很小——加料加在最前面,反而是最容易被快取的位置。

明天

Day15 是這系列的第一個「彈性格」:把額外的指令灌進 system prompt,和原版比較。我會先寫下事前預測,再看資料是不是打臉。


上一篇
Day13:多三個搜尋工具卻沒有更快:Agent 如何用 Bash 繞路
下一篇
Day15:規則該放 AGENTS.md 還是 System Prompt?20 次實驗比較
系列文
Harness Engineering × Pi Agent 實戰:打造可觀測、可評估的 AI Coding Agent 共 17 篇
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

0
WendyHus
iT邦新手 5 級 ‧ 2026-09-28 17:05:32

攤開的好棒!!!

我要留言

立即登入留言